package com.cn.xc.utils;

import java.util.StringTokenizer;

/**
 * FUN: String tools class
 * 
 * @author sushy
 * @date 2016-3-23
 */
public class StringUtil {

	public static final String EMPTY = "";

	/**
	 * //# check str whether is null or length=0
	 * 
	 * @param str
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	/**
	 * //# check str whether is not null and length !=0
	 * 
	 * @param str
	 */
	public static boolean isNotEmpty(String str) {
		return !StringUtil.isEmpty(str);
	}

	/**
	 * //# check str whether is null or length !=0 or include space
	 * 
	 * @param str
	 */
	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(str.charAt(i)) == false)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * //# check str whether is null and length !=0 and include space
	 * 
	 * @param str
	 */
	public static boolean isNotBlank(String str) {
		return !StringUtil.isBlank(str);
	}

	/**
	 * //# binary convert to hexadecimal
	 * 
	 * @param b
	 */
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}

		return hs.toUpperCase();
	}

	/**
	 * //# sqlit str
	 * 
	 * @param str
	 * @param sep
	 * @return
	 */
	public static String[] splitTokens(String str, String sep) {
		if (StringUtil.isEmpty(str)) {
			return null;
		}

		StringTokenizer token = new StringTokenizer(str, sep);
		int i = 0;
		int count = token.countTokens();
		if (count < 1) {
			return null;
		}

		String[] output = new String[count];
		while (token.hasMoreTokens()) {
			output[i] = token.nextToken();
			++i;
		}
		return output;
	}

	/**
	 * //# modify prperty to table columnName ，eg: attrCode - ATTR_CODE
	 * 
	 * @param columnName
	 */
	public static String parsePropertyName2ColumnName(String propertyName) {
		StringBuffer sb = new StringBuffer();
		char c = 0;
		char[] chs = propertyName.toCharArray();
		int len = chs.length;
		for (int i = 0; i < len; i++) {
			c = chs[i];
			if (Character.isUpperCase(c)) {// # upper
				sb.append("_");
				sb.append(Character.toLowerCase(c));// convert to lower
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * //# yourself see see ,its function is too so easy
	 * 
	 * @param input
	 * @param def
	 * @return
	 */
	public static String handleNULL(String input, String def) {
		if (null == input || input.length() <= 0) {
			return def;
		}
		String trimStr = input.trim();
		if (null == trimStr || trimStr.length() <= 0) {
			return def;
		}
		return trimStr;
	}

	/** please see up notes */
	public static boolean equals(String str1, String str2) {
		return str1 == null ? str2 == null : str1.equals(str2);
	}

	/** please see up notes again */
	public static boolean equalsIgnoreCase(String str1, String str2) {
		return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
	}

	/**
	 * //# delete \t \n \r of in str
	 * 
	 * @param str
	 */
	public static String trimAll(String str) {
		if (StringUtil.isEmpty(str)) {
			return str;
		}
		StringBuffer buf = new StringBuffer(str);
		int index = 0;
		while (buf.length() > index) {
			char c = buf.charAt(index);
			if (Character.isWhitespace(c) || c == '\t' || c == '\r' || c == '\n') {
				buf.deleteCharAt(index);
			} else {
				index++;
			}
		}
		return buf.toString();
	}

	/**
	 * //# delete \t \n \r of str around
	 * 
	 * @param str
	 * @return
	 */
	public static String trimMore(String str) {
		if (StringUtil.isEmpty(str)) {
			return str;
		}
		StringBuffer buf = new StringBuffer(str);

		// # head
		int index = 0;
		while (buf.length() > index) {
			char c = buf.charAt(index);
			if (Character.isWhitespace(c) || c == '\t' || c == '\r' || c == '\n') {
				buf.deleteCharAt(index);
			} else {
				break;
			}
		}

		// # foot
		while (buf.length() > 0) {
			int len = buf.length() - 1;
			char c = buf.charAt(len);
			if (Character.isWhitespace(c) || c == '\t' || c == '\r' || c == '\n') {
				buf.deleteCharAt(len);
			} else {
				break;
			}
		}

		return buf.toString();
	}

	/**
	 * //# well,so,I don't what to say at all. anyway, that's it. 
	 *    okey, I just say a little for you, its funtion is to avoid NullPointerException. 
	 *    if you still don't konw reason ,then, one make it, I go die.
	 *    I don't care, hehe.....2016.03.24 16:36:33
	 * 
	 * @param str
	 */
	public static String trim(String str) {
		if (isEmpty(str)) {
			return str;
		}
		return str.trim();
	}
}
